<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;

/**
 * PersoninfoSearch represents the model behind the search form about `app\models\Personinfo`.
 */
class IntegratedquerySearch extends IntegratedqueryModel
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['stu_id', 'stu_name', 'password_hash', 'card_id', 'used_name', 'nation', 'political', 'address', 'religion', 'height', 'maritial_status', 'blood_type', 'telephone', 'qq', 'blank', 'blank_num'], 'safe'],
            [['sex'], 'integer'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
//         $query = IntegratedqueryModel::find();

        // add conditions that should always apply here

//         $dataProvider = new ActiveDataProvider([
//             'query' => $query,
//         	'pagination' => [
//         				'pageSize' => 100,
//         		],
//         ]);
    	$sql1='select COUNT(*) from personinfo';
    	$sql2='SELECT p.stu_id,p.stu_name,p.sex,p.card_id,p.used_name,p.nation,p.political,p.address,p.religion,p.height,p.maritial_status,p.blood_type,p.telephone,p.qq,p.blank,p.blank_num,s.enroll_time,
s.stu_type,s.major_name,s.class_name,s.status,d.dorm_area,d.dormitory,d.dorm_num,d.leader,f.address,f.recipient,f.postal_code,f.economic_status,f.load FROM personinfo p LEFT JOIN schoolinfo s ON p.stu_id=s.stu_id LEFT JOIN dorminfo d ON d.stu_id=p.stu_id LEFT JOIN familyinfo f ON f.stu_id=p.stu_id';
    	$totalCount = Yii::$app->db->createCommand($sql1)->queryScalar();
        $dataProvider=new SqlDataProvider([
        		'sql'=>$sql2,
        		'totalCount'=>$totalCount,
        		'pagination' => [
        				'pageSize' => 100,
        		],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'sex' => $this->sex,
        ]);

        $query->andFilterWhere(['like', 'stu_id', $this->stu_id])
            ->andFilterWhere(['like', 'stu_name', $this->stu_name])            
            ->andFilterWhere(['like', 'password_hash', $this->password_hash])
            ->andFilterWhere(['like', 'card_id', $this->card_id])
            ->andFilterWhere(['like', 'used_name', $this->used_name])
            ->andFilterWhere(['like', 'nation', $this->nation])
            ->andFilterWhere(['like', 'political', $this->political])
            ->andFilterWhere(['like', 'address', $this->address])
            ->andFilterWhere(['like', 'religion', $this->religion])
            ->andFilterWhere(['like', 'height', $this->height])
            ->andFilterWhere(['like', 'maritial_status', $this->maritial_status])
            ->andFilterWhere(['like', 'blood_type', $this->blood_type])
            ->andFilterWhere(['like', 'telephone', $this->telephone])
            ->andFilterWhere(['like', 'qq', $this->qq])
            ->andFilterWhere(['like', 'blank', $this->blank])
            ->andFilterWhere(['like', 'blank_num', $this->blank_num]);
            

        return $dataProvider;
    }
}
